home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / iphdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-10  |  3.9 KB  |  181 lines

  1. /* @(#) $Header: iphdr.c,v 1.3 91/05/09 07:38:27 deyke Exp $ */
  2.  
  3. /* IP header conversion routines
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "ip.h"
  9. #include "internet.h"
  10.  
  11. /* Convert IP header in host format to network mbuf
  12.  * If cflag != 0, take checksum from structure,
  13.  * otherwise compute it automatically.
  14.  */
  15. struct mbuf *
  16. htonip(ip,data,cflag)
  17. register struct ip *ip;
  18. struct mbuf *data;
  19. int cflag;
  20. {
  21.     int16 hdr_len;
  22.     struct mbuf *bp;
  23.     register char *cp;
  24.     int16 fl_offs;
  25.  
  26.     hdr_len = IPLEN + ip->optlen;
  27.     if((bp = pushdown(data,hdr_len)) == NULLBUF){
  28.         free_p(data);
  29.         return NULLBUF;
  30.     }
  31.     cp = bp->data;
  32.  
  33.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  34.     *cp++ = ip->tos;
  35.     cp = put16(cp,ip->length);
  36.     cp = put16(cp,ip->id);
  37.     fl_offs = ip->offset >> 3;
  38.     if(ip->flags.congest)
  39.         fl_offs |= 0x8000;
  40.     if(ip->flags.df)
  41.         fl_offs |= 0x4000;
  42.     if(ip->flags.mf)
  43.         fl_offs |= 0x2000;
  44.  
  45.     cp = put16(cp,fl_offs);
  46.     *cp++ = ip->ttl;
  47.     *cp++ = ip->protocol;
  48.     if(cflag){
  49.         /* Use checksum from host structure */
  50.         cp = put16(cp,ip->checksum);
  51.     } else {
  52.         /* Clear checksum for later recalculation */
  53.         *cp++ = 0;
  54.         *cp++ = 0;
  55.     }
  56.     cp = put32(cp,ip->source);
  57.     cp = put32(cp,ip->dest);
  58.     if(ip->optlen != 0)
  59.         memcpy(cp,ip->options,ip->optlen);
  60.  
  61.     /* If requested, recompute checksum and insert into header */
  62.     if(!cflag)
  63.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  64.  
  65.     return bp;
  66. }
  67. /* Extract an IP header from mbuf */
  68. int
  69. ntohip(ip,bpp)
  70. register struct ip *ip;
  71. struct mbuf **bpp;
  72. {
  73.     int16 ihl;
  74.     int16 fl_offs;
  75.     char ipbuf[IPLEN];
  76.  
  77.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  78.         return -1;
  79.  
  80.     ip->version = (ipbuf[0] >> 4) & 0xf;
  81.     ip->tos = ipbuf[1];
  82.     ip->length = get16(&ipbuf[2]);
  83.     ip->id = get16(&ipbuf[4]);
  84.     fl_offs = get16(&ipbuf[6]);
  85.     ip->offset = fl_offs << 3;
  86.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  87.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  88.     ip->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  89.     ip->ttl = ipbuf[8];
  90.     ip->protocol = ipbuf[9];
  91.     ip->checksum = get16(&ipbuf[10]);
  92.     ip->source = get32(&ipbuf[12]);
  93.     ip->dest = get32(&ipbuf[16]);
  94.  
  95.     ihl = (ipbuf[0] & 0xf) << 2;
  96.     if(ihl < IPLEN){
  97.         /* Bogus packet; header is too short */
  98.         return -1;
  99.     }
  100.     ip->optlen = ihl - IPLEN;
  101.     if(ip->optlen != 0)
  102.         pullup(bpp,ip->options,ip->optlen);
  103.  
  104.     return ip->optlen + IPLEN;
  105. }
  106. /* Perform end-around-carry adjustment */
  107. int16
  108. eac(sum)
  109. register int32 sum;     /* Carries in high order 16 bits */
  110. {
  111.     register int16 csum;
  112.  
  113.     while((csum = sum >> 16) != 0)
  114.         sum = csum + (sum & 0xffffL);
  115.     return (int16) (sum & 0xffffl); /* Chops to 16 bits */
  116. }
  117. /* Checksum a mbuf chain, with optional pseudo-header */
  118. int16
  119. cksum(ph,m,len)
  120. struct pseudo_header *ph;
  121. register struct mbuf *m;
  122. int16 len;
  123. {
  124.     register int16 cnt, total;
  125.     register int32 sum, csum;
  126.     register char *up;
  127.     int16 csum1;
  128.     int swap = 0;
  129.  
  130.     sum = 0l;
  131.  
  132.     /* Sum pseudo-header, if present */
  133.     if(ph != NULLHEADER){
  134.         sum = hiword(ph->source);
  135.         sum += loword(ph->source);
  136.         sum += hiword(ph->dest);
  137.         sum += loword(ph->dest);
  138.         sum += uchar(ph->protocol);
  139.         sum += ph->length;
  140.     }
  141.     /* Now do each mbuf on the chain */
  142.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  143.         cnt = min(m->cnt, len - total);
  144.         up = (char *)m->data;
  145.         csum = 0;
  146.  
  147.         if(((long)up) & 1){
  148.             /* Handle odd leading byte */
  149.             if(swap)
  150.                 csum = uchar(*up++);
  151.             else
  152.                 csum = (int16)(uchar(*up++) << 8);
  153.             cnt--;
  154.             swap = !swap;
  155.         }
  156.         if(cnt > 1){
  157.             /* Have the primitive checksumming routine do most of
  158.              * the work. At this point, up is guaranteed to be on
  159.              * a short boundary
  160.              */
  161.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  162.             if(swap)
  163.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  164.             csum += csum1;
  165.         }
  166.         /* Handle odd trailing byte */
  167.         if(cnt & 1){
  168.             if(swap)
  169.                 csum += uchar(up[--cnt]);
  170.             else
  171.                 csum += (int16)(uchar(up[--cnt]) << 8);
  172.             swap = !swap;
  173.         }
  174.         sum += csum;
  175.         total += m->cnt;
  176.     }
  177.     /* Do final end-around carry, complement and return */
  178.     return (int16)(~eac(sum) & 0xffff);
  179. }
  180.  
  181.